/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.annuaireComp.sessions;

import fr.annuaireComp.entities.Competence;
import fr.annuaireComp.entities.DomaineCompetence;
import fr.annuaireComp.entities.SavoirCompetence;
import fr.annuaireComp.facades.CompetenceFacadeLocal;
import fr.annuaireComp.facades.DomaineCompetenceFacadeLocal;
import fr.annuaireComp.facades.SavoirCompetenceFacadeLocal;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author MiniK
 */
@Stateless
public class CompetencesManagerBean implements CompetencesManager, CompetencesManagerLocal {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @EJB
    private CompetenceFacadeLocal _DALcomp;
    @EJB
    private DomaineCompetenceFacadeLocal _DALdomaine;
    @EJB
    private SavoirCompetenceFacadeLocal _DALsavoir;

    @Override
    public void addCompetence(String libelle, Integer savoir, Integer domaine, Integer idCompetenceMere) {
        Competence c = new Competence();
        c.setLibelle(libelle);
        if (savoir != null) {
            c.setSavoirCompetence(new SavoirCompetence(savoir));
        }

        if (domaine != null) {
            c.setDomaineCompetence(new DomaineCompetence(domaine));
        }

        if (idCompetenceMere != null) {
            c.setCompetenceMere(new Competence(idCompetenceMere));
        }


        _DALcomp.create(c);
    }

    @Override
    public void editCompetence(Competence c) {

        if (c.getCompetenceMere() != null) {
            if (c.getCompetenceMere().getIdcompetence() == c.getIdcompetence()) {
                c.setCompetenceMere(null);
            }
        }
        _DALcomp.edit(c);
    }

    @Override
    public List<Competence> listCompetences() {
        return _DALcomp.findAll();
    }

    @Override
    public Competence getCompetence(int competenceId) {
        return _DALcomp.find(competenceId);
    }

    @Override
    public void addSavoir(String libelle) {
        SavoirCompetence s = new SavoirCompetence();
        s.setLibelle(libelle);
        _DALsavoir.create(s);
    }

    @Override
    public void addDomaine(String libelle) {
        DomaineCompetence d = new DomaineCompetence();
        d.setLibelle(libelle);
        _DALdomaine.create(d);
    }

    @Override
    public List<Competence> listCompetencesByDomaineLibelle(String domaineLibelle) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Competence> listCompetencesBy(Map<String, Object> criteria) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Competence> listCompetencesBySavoirLibelle(String savoirLibelle) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<SavoirCompetence> listSavoirs() {
        return _DALsavoir.findAll();
    }

    @Override
    public List<DomaineCompetence> listDomaines() {
        return _DALdomaine.findAll();
    }

    @Override
    public void deleteCompetence(int compId) {
        Competence comp = this.getCompetence(compId);
        _DALcomp.remove(comp);
    }
}
